<?php
ob_start();
session_start();

function __autoload($class_name) {
include './'.$class_name . '.php';
}

$buyerFirstname = $_GET['buyerFirstname'];
$buyerLastname = $_GET['buyerLastname'];
$buyerAddress1 = $_GET['buyerAddress1'];
$buyerAddress2 = $_GET['buyerAddress2'];
$buyerCity = $_GET['buyerCity'];
$buyerState = $_GET['buyerState'];
$buyerPincode = $_GET['buyerPincode'];
$buyerEmail = $_GET['buyerEmail'];
$buyerContactno = $_GET['buyerContactno'];
$cart = $_SESSION['cart'];
if(is_null($cart)){
echo -1;		
ob_end_flush();
}else{

$orders = "";

		$count = $cart -> countOrder();
		$orderList = $cart -> getOrderList();
		for ($i = 0; $i < $count; $i++) {
			$myOrder = $orderList[$i];
			$orders = $orders."(".$myOrder->getProductId().",";
			$orders = $orders.$myOrder->getQuantity().",";
			if($i != ($count-1)){
				$orders = $orders.$myOrder->getRate()."),";	
			}else {
				$orders = $orders.$myOrder->getRate().")";	
			}
		}

echo $orders;

$_SESSION['cart']=null;

$host="localhost"; // Host name 
$username="root"; // Mysql username  
$password="root123"; // Mysql password 
$db_name="hambaa_tshirts"; // Database name 

$mysqli = new mysqli($host, $username, $password, $db_name);
if ($mysqli->connect_errno) {
    die( "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
}

if (!$mysqli->query("DROP TABLE IF EXISTS details_order") ||
    !$mysqli->query("create TEMPORARY TABLE details_order(product_id BIGINT(50),quantity INT(5),price DOUBLE)") ||
    !$mysqli->query("INSERT INTO details_order(product_id,quantity,price) VALUES ".$orders)) {
    die( "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error);
}

if (!$mysqli->query("CALL create_order('".$buyerFirstname."','".$buyerLastname."','".$buyerAddress1."','".$buyerAddress2."','".$buyerCity."','".$buyerState."','".$buyerPincode."','".$buyerEmail."','".$buyerContactno."',@orderid)")) {
   die( "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error);
}

$orderId=$mysqli->query( 'select @orderid' );

while($row = $orderId->fetch_object())
{
print '<pre>';
print_r($row[0]->{'@orderid'});
print '</pre>';}


//$orderId=mysql_fetch_array($result_product);


mysqli_close($mysqli);


}
echo 'ddd'.$orderId.'eee';
ob_end_flush();
?>
